{extend name='./layout/_content_open'}

{block name='content_open'}
<div class="panel panel-success">
    <div class="panel-body">
        <form action="#" method="POST" class="form-horizontal" role="form" id="edit-form">
            <input type="hidden" name="id" id="id" value="{$role->id}">
            <div class="form-group">
                <label class="col-sm-2 control-label">角色名称</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="name" name="name"
                           placeholder="输入角色名称" required data-bv-notempty-message="角色名称不为空"
                           pattern="^.{2,10}$" data-bv-regexp-message="角色名称在两到十个字符之间"
                           value="{$role->name}"/>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">代码</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="code" name="code"
                           placeholder="输入代码，供程序调用" required data-bv-notempty-message="代码不为空"
                           pattern="^.{2,10}$" data-bv-regexp-message="代码在两到十个字符之间"
                           value="{$role->code}"/>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">描述</label>
                <div class="col-sm-8">
                    <textarea class="form-control" id="represent" name="represent"
                              placeholder="输入描述" required data-bv-notempty-message="描述不为空"
                              pattern="^.{2,1000}$" data-bv-regexp-message="描述在两到一千个字符之间">{$role->represent}</textarea>
                </div>
            </div>
        </form>
    </div>
    <div class="panel-footer">
        <div class="row">
            <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">

            </div>
            <div class="text-center col-xs-4 col-sm-4 col-md-4 col-lg-4">

            </div>
            <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 text-right">
                <button class="btn btn-success btn-sm" id="btn-edit" onclick="view.submit();">保存</button>
                <button class="btn btn-warning" id="btn-canal" onclick="parent.view.close();">取消</button>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            view.init();
        });
        var view = {
            init: function () {
                $('#edit-form').bootstrapValidator({
                    message: '值不能为空',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    }
                });

                $('#edit-form').on('submit', function () {
                    return false;
                });
            },
            submit: function () {
                $('#edit-form').data('bootstrapValidator').validate();
                var b = $('#edit-form').data('bootstrapValidator').isValid();
                if (!b) {
                    return false;
                }
                $('#btn-edit').attr('disabled', 'disabled');
                $.ajax({
                    url: "{:url('/sys/role/edit')}",
                    type: 'post',
                    dataType: 'json',
                    data: $('#edit-form').serialize(),
                    success: function (data) {
                        console.log(data);
                        if (data.status) {
                            layer.msg(data.msg, {icon: 1});
                            //wpgaci.goUrl("{:url('/sys/role/index')}", 1.5);
                            parent.view.table.bootstrapTable('refresh');
                            setTimeout(function () {
                                parent.view.close();
                            },1500);
                        } else {
                            layer.msg(data.msg, {icon: 2});
                            $('#btn-edit').removeAttr('disabled');
                        }
                    },
                    error: function () {
                        layer.msg('网络错误，请稍后再试', {icon: 5});
                        $('#btn-edit').removeAttr('disabled');
                    }
                });
            }
        };
    </script>
</div>
{/block}